package com.api.regression.group.editmember;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.MemberApi;
import com.zyncro.api.model.ObjectResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class EditMemberOwner extends AbstractApiTest {

	@Value("#{groupmembers['user.owner'].appId}")
	private String userUrn;

	@Value("#{groupmembers['user.owner'].password}")
	private String userPassword;

	@Value("#{groupmembers['group.toEdit'].urn}")
	private String groupUrn;

	@Value("#{groupmembers['user.toEdit'].appId}")
	private String memberToEditUrn;

	private boolean isEditor;
	private boolean isTaskManager;
	private boolean isInviter;
	private boolean isCommenter;
	private boolean isOwner;

	@BeforeClass
	public void loginUser() throws ZyncroApiException {

		getApi().login(userUrn, userPassword);

		MemberApi member = getApi().getService(IGroupApiService.class).getMember(groupUrn, memberToEditUrn).getObject();

		this.isEditor = member.isEditor();
		this.isTaskManager = member.isTaskManager();
		this.isInviter = member.isInviter();
		this.isCommenter = member.isCommenter();
		this.isOwner = member.isOwner();
	}

	@Test
	public void testEditMember() throws ZyncroApiException {

		getApi().getService(IGroupApiService.class).editMember(groupUrn, memberToEditUrn, !isEditor, !isTaskManager, !isInviter, !isCommenter,
				!isOwner);

		ObjectResultApi<MemberApi> result = getApi().getService(IGroupApiService.class).getMember(groupUrn, memberToEditUrn);
		MemberApi member = result.getObject();

		Assert.assertEquals(member.isEditor(), !isEditor, "Is editor permission");
		Assert.assertEquals(member.isTaskManager(), !isTaskManager, "Is task manager permission");
		Assert.assertEquals(member.isInviter(), !isInviter, "Is inviter permission");
		Assert.assertEquals(member.isCommenter(), !isCommenter, "Is commenter permission");
		Assert.assertEquals(member.isOwner(), !isOwner, "Is owner permission");

	}
}
